package poo.thread.counter;

public class User extends Thread {
	private int ID;
	private Counter c;
	private final int MIN, MAX;

	public User(int ID, Counter c, int MIN, int MAX) {
		this.ID = ID;
		this.c = c;
		this.MIN = MIN;
		this.MAX = MAX;
	}

	public void run() {
		while (!isInterrupted()) {
			if (Math.random() < 0.5) {
				c.incr();
				System.out.println("User#" + ID + " incr " + c);
			} else {
				c.decr();
				System.out.println("User#" + ID + " decr " + c);
			}
			pausa();
		}

	}

	private void pausa() {
		try {
			Thread.sleep((int) (Math.random() * (MAX - MIN)) + MIN); // genara
																		// un
																		// numero
																		// uniformemente
																		// distriubuito
																		// tra
																		// min e
																		// maz
																		// perche
																		// se
																		// esce
																		// zero
																		// vale
																		// min ecc
		} catch (InterruptedException e) {

		}

	}// pausa
}
